import { defineStore } from 'pinia'
import { axg_getUserTypeStatus } from './interface/User'
import axiosInstance from './network/axios'

export const useMainStore = defineStore('mainStore', {
    state: () => {
        return {
            app: {
                initlized: false,
                setting: default_client_setting, //store持久化、自动读取
                version: 'x.x.x'
            },
            session: {
                AES_KEY: '',    //unused

                user_mgmt: {
                    user_type_status: {
                        local: null as string | boolean | null,
                        virtual: null as string | boolean | null,
                        anonymous: null as string | boolean | null,
                        async handleUpdateUserTypeStatus(type: 'local' | 'virtual' | 'anonymous') {
                            this.anonymous = await axg_getUserTypeStatus(type)
                        }
                    }
                }
            }
        }
    },
    getters: {},
    actions: {
        switchTheme() {
            this.app.setting.ui.dark = !this.app.setting.ui.dark
        },
        fetchAppVersion() {
            console.log('fetch getting version from remote...')
            //fetch('https://gitee.com/valstrax/sea-archiver/raw/main/version.json')
            axiosInstance.get('/things?name=version')
                .then(async (res) => {
                    const t: any | object | string = await res.data
                    this.app.version = t
                })
                .catch(() => {})
        }
    }
})

export type ClientSetting = typeof default_client_setting
export const default_client_setting = {
    ui: {
        dark: false
    },
    page_login: {
        remember_me: false
    }
}
